package com.itestor.iptv.modules.alert.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.hibernate.Query;
import com.itestor.iptv.modules.alert.bean.RobotLogQuery;
import com.itestor.iptv.modules.alert.dao.TbRobotLogDao;
import com.itestor.iptv.modules.alert.dao.TbRobotLogDetailDao;
import com.itestor.iptv.modules.alert.domain.RobotLog;
import com.itestor.iptv.modules.alert.service.RobotLogService;
import com.itestor.iptv.modules.alert.vo.RobotLogBean;
import com.itestor.iptv.modules.alert.vo.RobotLogDetailBean;
import com.itestor.iptv.modules.alert.vo.RobotLogOBJ;
import com.itestor.iptv.utils.BeanConverter;

/**
 * 
 * @author Mr.li
 * 
 */
public class RobotLogServiceImpl implements RobotLogService {
    /**
     * Logger for this class
     */
    private static final Logger logger = Logger
            .getLogger(RobotLogServiceImpl.class);
    /**
     * 
     */
    private TbRobotLogDao tbRobotLogDao;
    /**
     * 
     */
    private TbRobotLogDetailDao tbRobotLogDetailDao;

    /**
     * @param robotLogQuery
     */
    @SuppressWarnings("unchecked")
    public void queryRobotLogList(RobotLogQuery robotLogQuery) {
        StringBuilder qcount_hql = new StringBuilder();
        StringBuilder hql = new StringBuilder();

        qcount_hql
                .append("select count(t.idRobotLog) from RobotLog t,RobotManage r,RobotFileInfo i where "
                        + " t.idRobot=r.idRobot and i.idRobot=t.idRobot and t.recycle=0");

        hql
                .append("select new com.itestor.iptv.modules.alert.vo.RobotLogBean(t,r,i) from RobotLog t,RobotManage r,RobotFileInfo i where "
                        + " t.idRobot=r.idRobot and i.idRobot=t.idRobot and t.recycle=0");

        if (null != robotLogQuery.getRobotName()) {
            qcount_hql.append(" and r.name like:name ");
            hql.append(" and r.name like:name ");
        }

        if (null != robotLogQuery.getConfirmStatus()) {
            qcount_hql.append(" and t.confirm =:confirm ");
            hql.append(" and t.confirm =:confirm ");
        }

        if (null != robotLogQuery.getQoememo()) {
            qcount_hql.append(" and t.qoeMomo like :qoeMomo ");
            hql.append(" and t.qoeMomo like :qoeMomo ");
        }

        if (null != robotLogQuery.getStartLogTime()) {
            qcount_hql.append(" and t.logTime >:startTime ");
            hql.append(" and t.logTime >:startTime ");
        }

        if (null != robotLogQuery.getEndLogTime()) {
            qcount_hql.append(" and t.logTime <:endTime ");
            hql.append(" and t.logTime <:endTime ");
        }

        qcount_hql.append(" order by r.name ,t.logTime desc ");
        hql.append(" order by t.logTime desc ,r.name");

        Query count_query = tbRobotLogDao.getHibernateSession().createQuery(
                qcount_hql.toString());
        Query query = tbRobotLogDao.getHibernateSession().createQuery(
                hql.toString());

        if (null != robotLogQuery.getRobotName()) {
            count_query.setParameter("name", "%" + robotLogQuery.getRobotName()
                    + "%");
            query
                    .setParameter("name", "%" + robotLogQuery.getRobotName()
                            + "%");
        }

        if (null != robotLogQuery.getConfirmStatus()) {
            count_query.setParameter("confirm", robotLogQuery
                    .getConfirmStatus());
            query.setParameter("confirm", robotLogQuery.getConfirmStatus());
        }

        if (null != robotLogQuery.getQoememo()) {
            count_query.setParameter("qoeMomo", "%"
                    + robotLogQuery.getQoememo() + "%");
            query.setParameter("qoeMomo", "%" + robotLogQuery.getQoememo()
                    + "%");
        }

        if (null != robotLogQuery.getStartLogTime()) {
            count_query.setParameter("startTime", robotLogQuery
                    .getStartLogTime());
            query.setParameter("startTime", robotLogQuery.getStartLogTime());
        }

        if (null != robotLogQuery.getEndLogTime()) {
            count_query.setParameter("endTime", robotLogQuery.getEndLogTime());
            query.setParameter("endTime", robotLogQuery.getEndLogTime());
        }

        // 获取记录总数.
        if (robotLogQuery.getPagerCount() == -1) {
            Long count = (Long) count_query.uniqueResult();
            robotLogQuery.setPagerCount(count.intValue());
        }

        query.setFirstResult(robotLogQuery.getPagerOffset());
        query.setMaxResults(robotLogQuery.getPagerPerPage());

        robotLogQuery.setQueryResults(query.list());
    }

    /**
     * 导出当前告警EXCEL用
     */
    public void getRobotLogList(RobotLogQuery robotLogQuery) {
        StringBuilder hql = new StringBuilder();
        hql.append("select new com.itestor.iptv.modules.alert.vo.RobotLogBean(t,r,i) from RobotLog t,RobotManage r,RobotFileInfo i where "
                        + " t.idRobot=r.idRobot and i.idRobot=t.idRobot and t.recycle=0");

        if (null != robotLogQuery.getRobotName()) {
            hql.append(" and r.name like:name ");
        }

        if (null != robotLogQuery.getConfirmStatus()) {
            hql.append(" and t.confirm =:confirm ");
        }

        if (null != robotLogQuery.getQoememo()) {
            hql.append(" and t.qoeMomo like :qoeMomo ");
        }
        
        if (null != robotLogQuery.getStartLogTime()) {
            hql.append(" and t.logTime >:startTime ");
        }

        if (null != robotLogQuery.getEndLogTime()) {
            hql.append(" and t.logTime <:endTime ");
        }
        
        hql.append(" order by t.logTime desc ,r.name");

        Query query = tbRobotLogDao.getHibernateSession().createQuery(
                hql.toString());

        if (null != robotLogQuery.getRobotName()) {
            query.setParameter("name", "%" + robotLogQuery.getRobotName()
                            + "%");
        }

        if (null != robotLogQuery.getConfirmStatus()) {
            query.setParameter("confirm", robotLogQuery.getConfirmStatus());
        }
        
        if (null != robotLogQuery.getQoememo()) {
            query.setParameter("qoeMomo", "%"+robotLogQuery.getQoememo()+"%");
        }

        if (null != robotLogQuery.getStartLogTime()) {
            query.setParameter("startTime", robotLogQuery.getStartLogTime());
        }

        if (null != robotLogQuery.getEndLogTime()) {
            query.setParameter("endTime", robotLogQuery.getEndLogTime());
        }
        robotLogQuery.setQueryResults(query.list());
    }
    
    
    /**
     * 查询历史告警日志列表
     * @param pRobotLogQuery
     */
    public void queryHisRobotLogList(RobotLogQuery robotLogQuery) {
        StringBuilder qcount_hql = new StringBuilder();
        StringBuilder hql = new StringBuilder();

        qcount_hql
                .append("select count(t.idRobotLog) from RobotLog t,RobotManage r,RobotFileInfo i where "
                        + " t.idRobot=r.idRobot and i.idRobot=t.idRobot and t.recycle=1");

        hql
                .append("select new com.itestor.iptv.modules.alert.vo.RobotLogBean(t,r,i) from RobotLog t,RobotManage r,RobotFileInfo i where "
                        + " t.idRobot=r.idRobot and i.idRobot=t.idRobot and t.recycle=1");

        if (null != robotLogQuery.getRobotName()) {
            qcount_hql.append(" and r.name like:name ");
            hql.append(" and r.name like:name ");
        }

        if (null != robotLogQuery.getConfirmStatus()) {
            qcount_hql.append(" and t.confirm =:confirm ");
            hql.append(" and t.confirm =:confirm ");
        }

        if (null != robotLogQuery.getQoememo()) {
            qcount_hql.append(" and t.qoeMomo like :qoeMomo ");
            hql.append(" and t.qoeMomo like :qoeMomo ");
        }

        if (null != robotLogQuery.getStartLogTime()) {
            qcount_hql.append(" and t.logTime >:startTime ");
            hql.append(" and t.logTime >:startTime ");
        }

        if (null != robotLogQuery.getEndLogTime()) {
            qcount_hql.append(" and t.logTime <:endTime ");
            hql.append(" and t.logTime <:endTime ");
        }

        qcount_hql.append(" order by r.name ,t.logTime desc ");
        hql.append(" order by t.logTime desc,r.name ");

        Query count_query = tbRobotLogDao.getHibernateSession().createQuery(
                qcount_hql.toString());
        Query query = tbRobotLogDao.getHibernateSession().createQuery(
                hql.toString());

        if (null != robotLogQuery.getRobotName()) {
            count_query.setParameter("name", "%" + robotLogQuery.getRobotName()
                    + "%");
            query
                    .setParameter("name", "%" + robotLogQuery.getRobotName()
                            + "%");
        }

        if (null != robotLogQuery.getConfirmStatus()) {
            count_query.setParameter("confirm", robotLogQuery
                    .getConfirmStatus());
            query.setParameter("confirm", robotLogQuery.getConfirmStatus());
        }

        if (null != robotLogQuery.getQoememo()) {
            count_query.setParameter("qoeMomo", "%"
                    + robotLogQuery.getQoememo() + "%");
            query.setParameter("qoeMomo", "%" + robotLogQuery.getQoememo()
                    + "%");
        }

        if (null != robotLogQuery.getStartLogTime()) {
            count_query.setParameter("startTime", robotLogQuery
                    .getStartLogTime());
            query.setParameter("startTime", robotLogQuery.getStartLogTime());
        }

        if (null != robotLogQuery.getEndLogTime()) {
            count_query.setParameter("endTime", robotLogQuery.getEndLogTime());
            query.setParameter("endTime", robotLogQuery.getEndLogTime());
        }

        // 获取记录总数.
        if (robotLogQuery.getPagerCount() == -1) {
            Long count = (Long) count_query.uniqueResult();
            robotLogQuery.setPagerCount(count.intValue());
        }

        query.setFirstResult(robotLogQuery.getPagerOffset());
        query.setMaxResults(robotLogQuery.getPagerPerPage());

        robotLogQuery.setQueryResults(query.list());
    }

    
    /**
     * 导出历史告警EXCEL用
     * @param pRobotLogQuery
     */
    public void getHisRobotLogList(RobotLogQuery robotLogQuery) {
        StringBuilder hql = new StringBuilder();

        hql.append("select new com.itestor.iptv.modules.alert.vo.RobotLogBean(t,r,i) from RobotLog t,RobotManage r,RobotFileInfo i where "
                        + " t.idRobot=r.idRobot and i.idRobot=t.idRobot and t.recycle=1");

        if (null != robotLogQuery.getRobotName()) {
            hql.append(" and r.name like:name ");
        }

        if (null != robotLogQuery.getConfirmStatus()) {
            hql.append(" and t.confirm =:confirm ");
        }
        
        if (null != robotLogQuery.getQoememo()) {
            hql.append(" and t.qoeMomo like :qoeMomo ");
        }
        
        if (null != robotLogQuery.getStartLogTime()) {
            hql.append(" and t.logTime >:startTime ");
        }

        if (null != robotLogQuery.getEndLogTime()) {
            hql.append(" and t.logTime <:endTime ");
        }

        hql.append(" order by t.logTime desc,r.name ");

        Query query = tbRobotLogDao.getHibernateSession().createQuery(
                hql.toString());

        if (null != robotLogQuery.getRobotName()) {
            query.setParameter("name", "%" + robotLogQuery.getRobotName()
                            + "%");
        }

        if (null != robotLogQuery.getConfirmStatus()) {
            query.setParameter("confirm", robotLogQuery.getConfirmStatus());
        }
        
        if (null != robotLogQuery.getQoememo()) {
            query.setParameter("qoeMomo", "%"+robotLogQuery.getQoememo()+"%");
        }
        
        if (null != robotLogQuery.getStartLogTime()) {
            query.setParameter("startTime", robotLogQuery.getStartLogTime());
        }

        if (null != robotLogQuery.getEndLogTime()) {
            query.setParameter("endTime", robotLogQuery.getEndLogTime());
        }

        robotLogQuery.setQueryResults(query.list());
    }
    
    /**
     * 获取告警详细信息
     * @param idRobotLog
     * @return List
     */
    public List<RobotLogDetailBean> getRobotLogDetailListByRobotLogId(
            Long idRobotLog) {
        List<RobotLogDetailBean> logDetailList = new ArrayList<RobotLogDetailBean>();
        logDetailList = tbRobotLogDetailDao
                .getRobotLogDetailListByRobotLogId(idRobotLog);
        return logDetailList;
    }

    /**
     * 获取20秒数据图片文件信息.
     * @param idRobotLogDetail
     * @return RobotLogDetailBean
     */
    public RobotLogDetailBean getRobotLogDetailVideoPicInfo(
            Long idRobotLogDetail) {
        return tbRobotLogDetailDao
                .getLogDetailBeanByLogDetailId(idRobotLogDetail);
    }

    /**
     * 
     * @param idRobotLog
     * @return
     */
    public RobotLogOBJ get(Long idRobotLog) {
        RobotLogOBJ robotLogOBJ = null;
        RobotLog robotLog = tbRobotLogDao.find(idRobotLog);

        if (robotLog != null) {
            robotLogOBJ = convertToRobotLogOBJ(robotLog);
        }
        return robotLogOBJ;
    }

    public void delete(long[] pIds) {
        for (long id : pIds) {
            tbRobotLogDao.delete(id);
        }
    }

    public void confirm(long[] pIds) {
        for (long id : pIds) {
            tbRobotLogDao.confirm(id);
        }
    }

    /**
     * @param trobotlog RobotLog(po)
     * @return robotlog RobotLogOBJ(vo)
     */
    public RobotLogOBJ convertToRobotLogOBJ(RobotLog trobotlog) {
        RobotLogOBJ robotlog = new RobotLogOBJ();
        BeanConverter.copyProperties(robotlog, trobotlog);
        return robotlog;
    }

    /**
     * 
     * @param idRobotLog
     * @return RobotLogBean
     */
    public RobotLogBean getRobotLogBean(Long idRobotLog) {
        StringBuilder hql = new StringBuilder();
        hql
                .append("select new com.itestor.iptv.modules.alert.vo.RobotLogBean(t,i) from RobotLog t,RobotFileInfo i where "
                        + " i.idRobot=t.idRobot and t.idRobotLog=:idRobotLog");

        Query query = tbRobotLogDao.getHibernateSession().createQuery(
                hql.toString());
        query.setParameter("idRobotLog", idRobotLog);

        return (RobotLogBean) query.uniqueResult();
    }

    /**
     * 获取最新的告警信息
     * @param startTime
     * @param endTime
     * @param topn 
     * @return List
     */
    @SuppressWarnings("unchecked")
    public List<RobotLogBean> getLatestRobotLog(Date startTime, Date endTime,
            Integer topn) {        
        StringBuffer hql = new StringBuffer();
        hql
                .append("select new com.itestor.iptv.modules.alert.vo.RobotLogBean(t,r,i) from RobotLog t,RobotManage r,RobotFileInfo i where "
                        + " t.idRobot=r.idRobot and i.idRobot=t.idRobot and t.recycle=0 ");
        if (null != startTime) {
            hql.append(" and t.logTime >:startTime");
        }

        if (null != endTime) {
            hql.append(" and t.logTime <:endTime");
        }

        hql.append(" order by r.name ,t.logTime desc ");

        Query query = tbRobotLogDao.getHibernateSession().createQuery(
                hql.toString());

        if (null != startTime) {
            query.setParameter("startTime", startTime.getTime() / 1000);
        }

        if (null != endTime) {
            query.setParameter("endTime", endTime.getTime() / 1000);
        }

        if (null != topn)
            query.setMaxResults(topn);
        
        return query.list();    
    }
    
    /**
     * 获取最新的告警次数
     * @param idRobotList 
     * @param startTime
     * @param endTime
     * @return List
     */
    public Map<Integer,Integer> getLatestRobotAlertTally(List<Integer> idRobotList,Date startTime,Date endTime){        
        StringBuffer hql = new StringBuffer();
        hql
                .append("select t.idRobot,count(t.idRobot) from RobotLog t,RobotManage r,RobotFileInfo i where "
                        + " t.idRobot=r.idRobot and i.idRobot=t.idRobot and t.recycle=0 ");
        
        if (null != idRobotList && !idRobotList.isEmpty()) {
            hql.append(" and t.idRobot in(:idList)");
        }
        
        if (null != startTime) {
            hql.append(" and t.logTime >:startTime");
        }

        if (null != endTime) {
            hql.append(" and t.logTime <:endTime");
        }
        
        hql.append(" group by t.idRobot order by r.name ,t.logTime desc ");

        Query query = tbRobotLogDao.getHibernateSession().createQuery(
                hql.toString());
        
        if (null != idRobotList && !idRobotList.isEmpty()) {
            query.setParameterList("idList",idRobotList);
        }
        
        if (null != startTime) {
            query.setParameter("startTime", startTime.getTime() / 1000);
        }

        if (null != endTime) {
            query.setParameter("endTime", endTime.getTime() / 1000);
        }
        
        List<Object[]>   return_list =query.list();
        
        Map<Integer,Integer> map=new HashMap<Integer, Integer>();
        for (Object[] objs : return_list ){
            Integer idRobot = (Integer)objs[0];
            Long count = (Long)objs[1];
            
            map.put(idRobot, count.intValue());            
        }
        
        return map;        
    }
    
    
    
    /**
     * 
     * @param robotlog
     * @return
     */
    public RobotLog convertToRegionManage(RobotLogOBJ robotlog) {
        RobotLog trobotlog = new RobotLog();
        BeanConverter.copyProperties(trobotlog, robotlog);
        return trobotlog;
    }

    public void setTbRobotLogDao(TbRobotLogDao pTbRobotLogDao) {
        tbRobotLogDao = pTbRobotLogDao;
    }

    public void setTbRobotLogDetailDao(TbRobotLogDetailDao pTbRobotLogDetailDao) {
        tbRobotLogDetailDao = pTbRobotLogDetailDao;
    }

}
